package cn.ffcs.organ.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.util.MyList;

/**
 * 组织action类<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class OrganizationAction<O extends Organization> extends BaseAction<O>
{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 业务POJO **/
    private O organization;
    
    /** path 指定返回页面 **/
    private String path="toTree";
  
    public String toTree(){
        return path;
    }
    /**
     * 产品类型树
     * @return
     */
    public String findTree(){
        ActionContext ctx = ActionContext.getContext();        
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        String type=request.getParameter("type");//radio,checkbox,null
        String module=request.getParameter("module");//organ,person
        List ls=super.findBy();
        String xml=MyList.getOrganTree(ls,type,module); 
        response.setContentType("text/xml;charset=gb2312");//页面文件头格式
        try{
        response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 新增或修改字典类型数据
     * @return
     */
    public String saveOrUpdate()
    {
        baseService.saveOrUpdate(organization);
        returnMsg="保存操作已成功！";
        return SUCCESS;
    }
    /**
     * 分页查找
     */
    public String findByPage(){
    	if (pageControl == null)
        {
            pageControl = new PageControl();
        }

        pageControl = baseService.findByPage(combineCond(),pageControl.getPageNo(),
                pageControl.getPageSize());

        return SUCCESS;
    }
    /**
     * 组合查询条件
     */
    @SuppressWarnings("unchecked")
	protected Criterion combineCond(){
        Criterion cond = null;
        HashMap<String,Object> conds=super.getConds();
        if (conds != null)
        {
            //遍历参数表
            Iterator i = conds.entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (conds.get(key) != null
                        && !StringUtils.isEmpty(conds.get(key).toString()))
                {
                    if (cond == null)
                    {
	                    if(key.indexOf("name")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else if(key.indexOf("orgType")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else {
	                		cond = Restrictions.eq(key, conds.get(key));
	                	}
                    }
                    else
                    {	
                    	if(key.indexOf("name")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else if(key.indexOf("orgType")!=-1){
	                		cond = Restrictions.like(key,"'%"+conds.get(key)+"%'");
	                	}else {
                    		cond = Restrictions.and(cond,  Restrictions.eq(key,conds.get(key)));
                    	}
                    }
                }
            }
        }
        return cond;
    }
    /**
     * 删除数据
     * @return
     */
    public String remove()
    {
        baseService.remove(organization);
        returnMsg="删除操作已成功！";
        return SUCCESS;
    }
 
    /**
     * 批量删除
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        returnMsg="批量删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 根据关键值获取实体数据
     * @return
     */
    public String findById()
    {
        if (organization != null && organization.getId() != 0)
        {
            organization = (O) baseService.findById(organization.getId());
        }     
        
        return SUCCESS;
    }
    
    public O getOrganization()
    {
        return organization;
    }

    public void setOrganization(O organization)
    {
        this.organization = organization;
    }
    public String getPath()
    {
        return path;
    }

    public void setPath(String path)
    {
        this.path = path;
    }

}
